package com.onLineCar.dispatch.entity.redis;

import cn.hutool.core.collection.CollectionUtil;
import lombok.Data;
import org.springframework.data.geo.GeoResult;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.redis.connection.RedisGeoCommands;

import java.util.ArrayList;
import java.util.List;

@Data
public class RedisGeoCommand {
    GeoLocation geoLocation;
    String distance;

    public static List<RedisGeoCommand> convertRedisGeoCommand(GeoResults<RedisGeoCommands.GeoLocation<String>> geoLocationGeoResults) {
        List<GeoResult<RedisGeoCommands.GeoLocation<String>>> content = geoLocationGeoResults.getContent();
        List<RedisGeoCommand> redisGeoCommands = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(content)) {
            content.forEach(f -> {
                RedisGeoCommand redisGeoCommand = new RedisGeoCommand();
                redisGeoCommand.setGeoLocation(new GeoLocation().setName(f.getContent().getName())
                        .setPoint(f.getContent().getPoint()));
                redisGeoCommand.setDistance(f.getDistance().toString());
                redisGeoCommands.add(redisGeoCommand);
            });
        }
        return redisGeoCommands;
    }
}
